<template>
  <div class="article-list">
    <van-pull-refresh
      v-model="isLoading"
      @refresh="onRefresh"
      :success-text="successText"
    >
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        :error.sync="error"
        error-text="请求失败，点击重新加载"
        @load="onLoad"
      >
        <van-cell v-for="article in list" :key="article.art_id">
          <div slot="title">
            <ArticleItem :article="article"></ArticleItem>
          </div>
        </van-cell>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { getArticle } from '@/api/article'
import ArticleItem from '@/components/article-item'
export default {
  name: 'ArticleList',
  components: {
    ArticleItem
  },
  props: {
    channel: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      list: [], // 储存数据
      loading: false, // 控制加载的状态
      finished: false, // 控制加载完成的状态
      pre_timestamp: null,
      error: false,
      count: 0,
      isLoading: false,
      successText: '刷新成功'
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onLoad () {
      //   // 异步更新数据
      //   // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      //   setTimeout(() => {
      //     for (let i = 0; i < 10; i++) {
      //       this.list.push(this.list.length + 1);
      //     }

      //     // 加载状态结束
      //     this.loading = false;

      //     // 数据全部加载完成
      //     if (this.list.length >= 40) {
      //       this.finished = true;
      //     }
      //   }, 1000);

      try {
        const res = await getArticle({
          channel_id: this.channel.id,
          timestamp: this.pre_timestamp || Date.now(),
          with_top: 1
        })

        // 手动抛出一个错误
        // if (Math.random() > 0.7) {
        //   throw new Error("错误");
        // }

        console.log(res)
        // 将数据追加到 list
        const { results } = res.data.data
        this.list = [...this.list, ...results]
        // 将loading 设置为false
        this.loading = false
        // 将更新的时间戳
        this.pre_timestamp = res.data.data.pre_timestamp
        // 判断是否加载完成
        if (res.data.data.pre_timestamp === null) {
          this.finished = true
        }
      } catch (error) {
        this.loading = false
        this.error = true
        this.$toast('获取失败')
      }
    },

    async onRefresh () {
      //   setTimeout(() => {
      //     this.$toast("刷新成功");
      //     this.isLoading = false;
      //     this.count++;
      //   }, 1000);
      try {
        const res = await getArticle({
          channel_id: this.channel.id,
          timestamp: Date.now(),
          with_top: 1
        })

        // 手动抛出一个错误
        // if (Math.random() > 0.7) {
        //   throw new Error("错误");
        // }

        console.log(res)
        // 将数据追加到 list
        const { results } = res.data.data
        this.list = [...results, ...this.list]
        this.successText = `刷新了${results.length}条数据`
        // 将loading 设置为false
        this.isLoading = false
      } catch (error) {
        this.isLoading = false
        this.$toast('刷新错误')
      }
    }
  }
}
</script>
<style lang="less" scoped>
.article-list {
  // 百分比单位是相对于父元素的
  // height: 100%;

  // 视口（在移动端是布局视口）单位：vw 和 vh，不受父元素影响
  // 1vw = 视口宽度的百分之一
  // 1vh = 视口高度的百分之一
  height: 79vh;
  overflow-y: auto;
}
</style>
